package com.zero.qth.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zero.common.core.domain.page.PageDomain;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zero.common.annotation.log.Log;
import com.zero.common.core.basecontroller.BaseController;
import com.zero.common.core.domain.result.AjaxResult;
import com.zero.common.core.enums.BusinessType;
import com.zero.qth.domain.Coupon;
import com.zero.qth.service.ICouponService;
import com.zero.common.core.utils.poi.ExcelUtil;
import com.zero.common.core.domain.page.TableDataInfo;

/**
 * 优惠券列表Controller
 * 
 * @author cai-xu-kun
 * @date 2025-08-27
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "优惠券列表")
@RequestMapping("/qth/coupon")
public class CouponController extends BaseController {

    private final ICouponService couponService;

    /**
     * 查询优惠券列表分页
     */
    @ApiOperation("查询优惠券列表分页")
    @PreAuthorize("@ss.hasPermi('qth:coupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(Coupon coupon) {
        PageDomain pageDomain = this.startPage();
        Page<Coupon> p = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
        Page<Coupon> page = couponService.selectCouponListPage(coupon, p);
        return this.getDataTable(page);
    }

    /**
     * 导出优惠券列表列表
     */
    @ApiOperation("导出优惠券列表列表")
    @PreAuthorize("@ss.hasPermi('qth:coupon:export')")
    @Log(title = "优惠券列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Coupon coupon) {
        List<Coupon> list = couponService.selectCouponList(coupon);
        ExcelUtil<Coupon> util = new ExcelUtil<Coupon>(Coupon.class);
        util.exportExcel(response, list, "优惠券列表数据");
    }

    /**
     * 获取优惠券列表详细信息
     */
    @ApiOperation("获取优惠券列表详细信息")
    @PreAuthorize("@ss.hasPermi('qth:coupon:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(couponService.selectCouponById(id));
    }

    /**
     * 新增优惠券列表
     */
    @ApiOperation("新增优惠券列表")
    @PreAuthorize("@ss.hasPermi('qth:coupon:add')")
    @Log(title = "优惠券列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Coupon coupon) {
        return toAjax(couponService.insertCoupon(coupon));
    }

    /**
     * 修改优惠券列表
     */
    @ApiOperation("修改优惠券列表")
    @PreAuthorize("@ss.hasPermi('qth:coupon:edit')")
    @Log(title = "优惠券列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Coupon coupon) {
        return toAjax(couponService.updateCoupon(coupon));
    }

    /**
     * 删除优惠券列表
     */
    @ApiOperation("删除优惠券列表")
    @PreAuthorize("@ss.hasPermi('qth:coupon:remove')")
    @Log(title = "优惠券列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(couponService.deleteCouponByIds(ids));
    }
}
